﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Practice.Domain.Shared;
using Practice.DTO;
using Practice.PH4_2104A.IService;

namespace Practice.API.Controllers
{
    /// <summary>
    /// 订单控制器
    /// </summary>
    [Route("order")]
    [ApiController]
    public class OrderController : ControllerBase
    {
        IOrderMasterService _service;

        public OrderController(IOrderMasterService service)
        {
            _service = service;
        }


        /// <summary>
        /// //统计订单的商品销售数量
        /// </summary>
        /// <returns></returns>        
        [HttpGet("sale")]
        public IActionResult GetSaleNumber()
        {
            var data = _service.GetOrderDetail();
            return Ok(data);
        }


        /// <summary>
        /// 文件导出
        /// </summary>
        /// <returns></returns>
        [HttpGet("export")]
        public IActionResult Export()
        {
            var data = _service.GetAll(1, 100, out int total);

            //把数据表的数据，放到excel对应的单元格即可
            // excel 97-03版本
            //NPOI.HSSF
            //获取工作簿对象
            NPOI.XSSF.UserModel.XSSFWorkbook wookBook = new NPOI.XSSF.UserModel.XSSFWorkbook("wwwroot/order_template.xlsx");

            //用工作簿获取里面的sheet对象
            var sheet = wookBook.GetSheetAt(0);
            int rowIndex = 2;
            data.ToList().ForEach(x =>
            {
                //创建行
                var row = sheet.CreateRow(rowIndex);
                //在行中，创建列，并为这一列赋值
                row.CreateCell(0).SetCellValue(rowIndex - 1);
                row.CreateCell(1).SetCellValue(x.OrderStateLabel);
                row.CreateCell(2).SetCellValue(x.OrderTimeLabel);
                row.CreateCell(3).SetCellValue(x.OrderAmount.ToString());

                rowIndex++;
            });

            MemoryStream ms = new MemoryStream();
            wookBook.Write(ms);

            return File(ms.ToArray(), "Application/excel",$"{ System.DateTime.Now.ToString("yyyyMMddhhmmss")}.xlsx");
        }

        /// <summary>
        /// 创建订单
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Insert(OrderInputDTO dto)
        {
            ReseponseResultDTO reseponse = new();

            _service.Insert(dto);

            return Ok(reseponse);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Get()
        {
            ReseponseResultDTO response = new();
            var data = _service.GetAll(1,10,out int total);
            response.Data = data;
            return Ok(response);
        }
        /// <summary>
         /// 查询一条
         /// </summary>
         /// <returns></returns>
        [HttpGet("first")]
        public IActionResult Get(string orderNo)
        {
            ReseponseResultDTO response = new();
            var data = _service.GetFirstByOrderNo(orderNo);
            response.Data = data;
            return Ok(response);
        }

        /// <summary>
        /// 订单状态枚举
        /// </summary>
        /// <returns></returns>
        [HttpGet("state")]
         public IActionResult GetState()
        {
            //1、获取枚举类型
            var e = typeof(OrderStateEnum);

            //2、获取这个枚举类型的字段
            var fs = e.GetFields();

            //3、将枚举字段对应的名称和值放到一个集合中，共下拉绑定
            List<dynamic> list = new List<dynamic>();
            fs.ToList().ForEach(x => {
               
                if(!x.Name.Equals("value__"))
                {
                    string name = x.Name;
                    var objs = x.GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.DisplayAttribute), true);
                    if(objs != null && objs.Length > 0)
                    {
                        name = ((System.ComponentModel.DataAnnotations.DisplayAttribute)objs[0]).Name;
                    }

                    list.Add(new
                    {
                        Name = name,
                        Value = (int)Enum.Parse(e,x.Name)
                    });
                }
            });

            return Ok(list);
        }

    }
}
